﻿namespace StudentProject
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using ExtensionMethods;

    public class MainStudentProject
    {
        //// 1.2.16.09. Create a class student with properties FirstName, LastName, FN, Tel, Email, Marks (a List<int>),
        //// GroupNumber. Create a List<Student> with sample students. Select only the students that are from group number 2.
        //// Use LINQ query. Order the students by FirstName.
        //// 1.2.16.10. Implement the previous using the same query expressed with extension methods.
        //// 1.2.16.11. Extract all students that have email in abv.bg. Use string methods and LINQ.
        //// 1.2.16.12. Extract all students with phones in Sofia. Use LINQ.
        //// 1.2.16.13. Select all students that have at least one mark Excellent (6) into a new anonymous class that
        //// has properties – FullName and Marks. Use LINQ.
        //// 1.2.16.14. Write down a similar program that extracts the students with exactly  two marks "2". Use extension methods.
        //// 1.2.16.15. Extract all Marks of the students that enrolled in 2006. (The students from
        //// 2006 have 06 as their 5-th and 6-th digit in the FN).
        //// 1.2.16.16. Create a class Group with properties GroupNumber and DepartmentName. Introduce a property Group in
        //// the Student class. Extract all students from "Mathematics" department. Use the Join operator.
        //// 1.2.16.18. Create a program that extracts all students grouped by GroupName and then prints them to the console. Use LINQ.
        //// 1.2.16.19. Rewrite the previous using extension methods.

        private static void Main()
        {
            var mathGroup = new Group(2, "Mathematics");
            var informaticsGroup = new Group(1, "Informatics");
            var groups = new List<Group> { mathGroup, informaticsGroup };

            var students = new List<Student>
            {
                new Student
                {
                    FN = 2020201,
                    FirstName = "Georgi",
                    LastName = "Georgiev",
                    Tel = "+359 2 123 123",
                    Email = "boringExercise@mail.bg",
                    Group = mathGroup,
                    Marks = new List<int> { 2, 2, 2, 2 }
                },

                new Student
                {
                    FN = 2020202,
                    FirstName = "Boris",
                    LastName = "Borisov",
                    Tel = "02 123 123",
                    Email = "Ani89@abv.bg",
                    Group = mathGroup,
                    Marks = new List<int> { 6, 6 }
                },

                new Student
                {
                    FN = 2020203,
                    FirstName = "Petur",
                    LastName = "Petrov",
                    Tel = "0888 123 123",
                    Email = "peturPletPlete@abv.bg",
                    Group = informaticsGroup,
                    Marks = new List<int> { 2, 2, 3 }
                },

                new Student
                {
                    FN = 2020204,
                    FirstName = "Ivaylo",
                    LastName = "Georgiev",
                    Tel = "0123 123 123",
                    Email = "sadMail@abv.bg",
                    Group = informaticsGroup,
                    Marks = new List<int> { 6, 6, 5, 5, 6 }
                }
            };

            var sortedStudentFromGroupTwo =
                from student in students
                where student.Group.GroupNumber == 2
                orderby student.FirstName
                select student;

            sortedStudentFromGroupTwo.PrintCollection();

            var sortedStudentsFromGroupTwoSecond = students
                .Where(st => st.Group.GroupNumber == 2)
                .OrderBy(st => st.FirstName);

            sortedStudentsFromGroupTwoSecond.PrintCollection();

            var abvEmailStudents = students.Where(st => st.Email.Contains("@abv.bg"));

            abvEmailStudents.PrintCollection();

            var sofiaPhoneStudents = students.Where(st =>
                st.Tel.StartsWith("02") ||
                st.Tel.StartsWith("+359 2"));

            sofiaPhoneStudents.PrintCollection();

            var excellentStudent =
                from student in students
                where student.Marks.Contains(6)
                select new { Fullname = student.FirstName + " " + student.LastName, Marks = string.Join(",", student.Marks) };

            excellentStudent.PrintCollection();

            var studentsWithTwoMark = students
                .Where(st => st.Marks.Count == 2)
                .Select(st => new { Name = st.FirstName + " " + st.LastName, Marks = string.Join(",", st.Marks) });

            studentsWithTwoMark.PrintCollection();

            string marksOfStudentsBornIn2006 = string.Join(
                ",",
                students
                .Where(st => st.FN
                    .ToString()
                    .Substring(4, 2)
                    .Equals("06"))
                .Select(st => string.Join(",", st.Marks)));

            Console.WriteLine(marksOfStudentsBornIn2006);

            var mathStudents =
                from study in groups
                where study.DepartmentName == "Mathematics"
                join student in students on study.GroupNumber equals student.Group.GroupNumber
                select new { study.DepartmentName, student };

            mathStudents.PrintCollection();

            var studentsByGroups =
                from student in students
                group student by student.Group.DepartmentName;

            foreach (var course in studentsByGroups)
            {
                Console.WriteLine(course.Key);

                foreach (var student in course)
                {
                    Console.WriteLine(student);
                }
            }

            Console.WriteLine(new string('-', 40));

            var groupsWithStudents = students.GroupBy(st => st.Group.DepartmentName);

            foreach (var course in groupsWithStudents)
            {
                Console.WriteLine(course.Key);

                foreach (var student in course)
                {
                    Console.WriteLine(student);
                }
            }
        }
    }
}